package com.qiangqiang.resoldhouse.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qiangqiang.resoldhouse.dto.AppointmentDTO;
import com.qiangqiang.resoldhouse.dto.PageDTO;
import com.qiangqiang.resoldhouse.entity.Appointment;
import com.qiangqiang.resoldhouse.entity.AppointmentSelect;
import com.qiangqiang.resoldhouse.mapper.AppointmentMapper;
import com.qiangqiang.resoldhouse.util.SnowflakeIdWorker;
import com.qiangqiang.resoldhouse.util.TimeFormat;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: zqh
 * @date: 2018/8/14 9:26
 * @description:
 */
@Service
public class AppointmentService {

    @Autowired
    private AppointmentMapper mapper;

    @Autowired
    private HouseResourceService houseResourceService;

    @Autowired
    private AgentService agentService;

    @Autowired
    private UserService userService;

    @Autowired
    private CommentService commentService;

    public Appointment findAppointmentById(Long appointmentId){
        return mapper.selectById(appointmentId);
    }

    public boolean addAppointment(Appointment appointment){
        appointment.setAppointmentId(new SnowflakeIdWorker(5,1).nextId());
//        System.out.println(appointment);
        return mapper.insert(appointment) > 0;
    }

    public Boolean updateAppointment(Appointment appointment){
        return mapper.updateById(appointment) > 0;
    }

    //根据页码和选择条件取得页面内容
    public PageDTO<AppointmentDTO> findPage(Integer pageNum, AppointmentSelect select) {
        //设置当前时间，用来将已处理的和未处理的预约分开
        select.setNowTime(Instant.now().toEpochMilli());

        PageDTO<AppointmentDTO> pageDTO = new PageDTO<>();
        Page<AppointmentDTO> page = new Page<>(pageNum, 5);
        List<AppointmentDTO> result = mapper.selectPage(page, getWrapper(select)).stream()
                .map(this::beanToDTO).collect(Collectors.toList());
        pageDTO.setPage(page);
        pageDTO.setResult(result);
        return pageDTO;
    }

    public Wrapper<Appointment> getWrapper(AppointmentSelect select){
        if(select == null) return null;

        Wrapper<Appointment> wrapper = new EntityWrapper<>();
        boolean has = false;

        if(select.getAgentId() != null){
            if(has) wrapper = wrapper.andNew();
            has = true;
            wrapper = wrapper.eq("agent_id", select.getAgentId());
        }
        if(select.getUserId() != null){
            if(has) wrapper = wrapper.andNew();
            has = true;
            wrapper = wrapper.eq("user_id", select.getUserId());
        }
        if(select.getFinished() != null && select.getNowTime() != null){
            if(has) wrapper = wrapper.andNew();
            has = true;
            //计算时间延后一天。。。。
            Long rTime = select.getNowTime() - 24*60*60*1000;
            if(select.getFinished()) wrapper = wrapper.lt("order_time", rTime);
            else wrapper = wrapper.ge("order_time", rTime);
        }
        if(select.getCommented() != null){
            if(has) wrapper = wrapper.andNew();
            has = true;
            if(select.getCommented())
                wrapper = wrapper.isNotNull("comment_id");
            else
                wrapper = wrapper.isNull("comment_id");
        }

        wrapper = wrapper.orderBy("order_time",false);

        return wrapper;
    }

    public AppointmentDTO beanToDTO(Appointment appointment){
        AppointmentDTO dto = new AppointmentDTO();
        BeanUtils.copyProperties(appointment, dto);
        dto.setOrderTime(TimeFormat.converTime(appointment.getOrderTime()));
        dto.setAgent(agentService.findAgentDTOById(dto.getAgentId()));
        dto.setHouse(houseResourceService.findHouseById(dto.getHouseId()));
        dto.setUser(userService.findUserById(dto.getUserId()));
        if(dto.getCommentId() != null)
            dto.setComment(commentService.findCommentByCommentId(dto.getCommentId()));
        dto.setFinished(appointment.getOrderTime() <= Instant.now().toEpochMilli() - 24*60*60*1000);
        return dto;
    }
}
